
package cz.czu.pef.DataModelling.beans;

import java.util.List;
import java.util.logging.*;

import javax.annotation.*;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cz.czu.pef.DataModelling.domainObjects.Company;
import cz.czu.pef.DataModelling.ejb.api.CompanyEjbInterface;
import cz.czu.pef.common.CustomFilterField;
import cz.czu.pef.common.CustomSortField;

@Component
@ViewScoped
@ManagedBean
@Scope(value="view")
public class CompanyBean extends AbstractExtendedTableBean<Company>
{
	private static final long serialVersionUID = -3072548005849148994L;

	public static final Logger logger = Logger.getLogger(CompanyBean.class.getName());

	private Company company;
	@Autowired private CompanyEjbInterface companyEjb;

	public CompanyBean()
	{
		super();
	}

	@PostConstruct
	public void create()
	{
	  //companyEjb = this.applicationContext.getBean(CompanyEjbInterface.class);
	  company = new Company();
	}

	public Company getCompany()
	{
		return company;
	}

	public void setCompany(Company company)
	{
		this.company = company;
	}

	public String createNew()
	{
		try
		{
			logger.finest("createNew():: Entry");
			/*
			FacesContext context = FacesContext.getCurrentInstance();
			//PersonImpl person = (PersonImpl)context.getApplication().evaluateExpressionGet(context, "#{person}", PersonImpl.class);
			PersonImpl newPerson = (PersonImpl) context.getApplication().getExpressionFactory().createValueExpression(context.getELContext(), "#{person}", PersonImpl.class).getValue(context.getELContext());
			*/

			logger.info("createNew():: company=" + company);
			if (company != null)
			{
				// persist new person
			    companyEjb.merge(company);
			}

			// reset detail form's data
			company = this.applicationContext.getBean(Company.class);

			return null;
		}
		finally
		{
			logger.finest("createNew():: Exit");

		}
	}

  @Override
  public boolean canBeRemoved(Company company)
  {
    return companyEjb.canBeRemoved(company);
  }

  @Override
  public void removeById(long id)
  {
    companyEjb.removeById(id);
  }

  @Override
  public Company getByStringId(String myKey)
  {
    return companyEjb.getByStringId(myKey);
  }

  @Override
  public List<Company> findAllByRangeWithFilterAndSort(Integer firstRow, Integer endRow, List<CustomFilterField> filterFields, List<CustomSortField> sortFields)
  {
    return companyEjb.findAllByRangeWithFilterAndSort(firstRow, endRow, filterFields, sortFields);
  }

  @Override
  public Long countWithFilter(List<CustomFilterField> filterFields)
  {
    return companyEjb.countWithFilter(filterFields);
  }
}
